package com.learn.annotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * @author chenxin
 * @time 2016-12-29-17:17
 */
@Component
@Aspect @Order(value = 1000)
public class TransferTypeAdvice {
    @Pointcut("@annotation(com.learn.annotation.TransferType)") public void point() {
    }

    @Around("point()") public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Object result = pjp.proceed();
        try {
            System.out.println("执行注解TransferType");
            Object target = pjp.getTarget();
            Signature signature = pjp.getSignature();
            if (signature instanceof MethodSignature) {
                MethodSignature methodSignature = (MethodSignature) signature;
                Method method = ReflectionUtils.findMethod(target.getClass(), methodSignature.getName(), methodSignature.getParameterTypes());
                //获取方法上的注解
                Annotation annotation = method.getAnnotation(TransferType.class);
                if(null != annotation){
                    TransferType transferType = (TransferType)annotation;
                    System.out.println(transferType.value());
                }

            }
        }  catch (Exception e) {
            e.printStackTrace();
        }
        return new Object();
    }
}
